package com.blockmeta.bbs.businesslibrary.dialog.poupwindow;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.blockmeta.bbs.baselibrary.utils.DimentionsUtil;
import com.blockmeta.bbs.businesslibrary.R;

/**
 * Created by author on 2018/5/10.
 * 封装全局在任意控件位置展示的PopupWindow
 * <p>
 * 关于PopupWindow的使用说明
 * https://juejin.im/post/58ed82c3a22b9d0063469e98
 * 第二部 添加动画
 */

public class LocationPopupWindow {


    /**
     * @param view           呈现的View视图
     * @param width          创建popupWindow的高
     * @param height         创建popupWindow的宽
     * @param location       在某个控件下面
     * @param locationWidth  宽度
     * @param locationHeight 距离顶部的高度
     * @param dismissView    可以为空 点击消失的控件
     * @return
     */
    public static PopupWindow initPopup(View view, int width, int height, View location
                                               , int locationWidth, int locationHeight, View dismissView) {
        final PopupWindow sPopupWindow = new PopupWindow(view, width, height, true);
        sPopupWindow.setFocusable(true);
        //设置pop点击外面消失
        sPopupWindow.setOutsideTouchable(true);

        sPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        //popup显示的位置
        sPopupWindow.showAsDropDown(location, locationWidth, locationHeight);
        sPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                sPopupWindow.dismiss();
            }
        });
        if (dismissView != null) {
            dismissView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    sPopupWindow.dismiss();
                }
            });
        }
        sPopupWindow.setAnimationStyle(R.style.AnimBottom);
        return sPopupWindow;
    }

//    public static PopupWindow initRankChoicePopup(View view, View location, final ImageView img
//                                                         , int locationWidth, View dismissView) {
//        final PopupWindow sPopupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT - 83, true);
//        sPopupWindow.setFocusable(true);
//        //设置pop点击外面消失
//        sPopupWindow.setOutsideTouchable(true);
//        sPopupWindow.setBackgroundDrawable(new BitmapDrawable());
//        //popup显示的位置  偏移坐标
//        sPopupWindow.showAsDropDown(location, locationWidth, 0);
//        sPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
//            @Override
//            public void onDismiss() {
//                sPopupWindow.dismiss();
//                img.setImageResource(R.drawable.home_choice_icon);
//            }
//        });
//        if (dismissView != null) {
//            dismissView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    sPopupWindow.dismiss();
//                    img.setImageResource(R.drawable.home_choice_icon);
//
//                }
//            });
//        }
//        sPopupWindow.setAnimationStyle(R.style.AnimBottom);
//        return sPopupWindow;
//    }

    /**
     * Coins 页面的筛选popupwindow
     *
     * @param mContext
     * @param view
     * @param locationView
     * @param img
     * @return
     */
    public static PopupWindow initRank(Context mContext, View view, View locationView, final ImageView img) {
        int width = DimentionsUtil.getWindowWith(mContext);
        int height = DimentionsUtil.getWindowHeight(mContext) - DimentionsUtil.dp2px(mContext, 82f) - DimentionsUtil.getStatusBarHeight(mContext);
        final PopupWindow popupWindow = new PopupWindow(view, width, height, true);
        popupWindow.setFocusable(true);
        //设置pop点击外面消失
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());

        popupWindow.showAsDropDown(locationView, width, 0);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                popupWindow.dismiss();
                if (img!=null){
                    img.setImageResource(R.drawable.home_choice_icon);
                }
            }
        });
        return popupWindow;
    }

}
